﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.Net.Mime;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.调拨管理
{
    /// <summary>
    /// 装备跨单位调拨申请
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class EquipmentUnitsAllocateController : BaseController
    {
        private readonly IEquipmentUnitsAllocateService equipmentUnitsAllocateService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="equipmentUnitsAllocateService"></param>
        public EquipmentUnitsAllocateController(IWebHostEnvironment environment, IEquipmentUnitsAllocateService equipmentUnitsAllocateService) : base(environment)
        {
            this.equipmentUnitsAllocateService = equipmentUnitsAllocateService;
        }
        /// <summary>
        /// 查询装备跨单位调拨申请
        /// </summary>
        /// <param name="name">调拨装备的名称</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllEquipmentUnitsAllocate(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = equipmentUnitsAllocateService.GetEquipmentUnitsAllocate(name, pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据装备跨单位调拨申请id查询装备跨单位调拨申请
        /// </summary>
        /// <param name="id">装备跨单位调拨申请id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipmentUnitsAllocateById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = equipmentUnitsAllocateService.GetEquipmentUnitsAllocateById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加装备跨单位调拨申请
        /// </summary>
        /// <param name="equipmentUnitsAllocate">装备跨单位调拨申请实体类</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddEquipmentUnitsAllocate(EquipmentUnitsAllocate equipmentUnitsAllocate)
        {
            if (string.IsNullOrEmpty(equipmentUnitsAllocate.Name) || equipmentUnitsAllocate.Name.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "名称不能为空！"));
            }
            equipmentUnitsAllocate.CreateUserId = base.BaseGetNowUserId();
            equipmentUnitsAllocate.CreateUserName = base.BaseGetNowUserName();

            var res = equipmentUnitsAllocateService.AddEquipmentUnitsAllocate(equipmentUnitsAllocate);
            if (res)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败", "500"));
        }
        /// <summary>
        /// 修改装备跨单位调拨申请
        /// </summary>
        /// <param name="equipmentUnitsAllocate">装备跨单位调拨申请实体类</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdEquipmentUnitsAllocate(EquipmentUnitsAllocate equipmentUnitsAllocate)
        {
            if (equipmentUnitsAllocate.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(equipmentUnitsAllocate.Name))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            var res = equipmentUnitsAllocateService.UpdEquipmentUnitsAllocate(equipmentUnitsAllocate);
            if (res)
            {          
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }
        /// <summary>
        /// 删除装备跨单位调拨申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelEquipmentUnitsAllocate(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = equipmentUnitsAllocateService.DelEquipmentUnitsAllocate(id);
            if (res)
            {         
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
    }
}
